package jinyilw.swing.visuals.Icomp;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.rlaf.theme.RThemeColor;

import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class IRichTextLabel extends ILabel
{
	private static final long serialVersionUID = 8425186977448479454L;
	private String text;
	private int hy;
	private float leftX = 0, leftY = 0, rightX = 1, rightY = 1;
	private Color leftColor, rightColor /* = darkColor */;
	private final Dimension preferredSize = new Dimension();
	private boolean autoShadow = true;
	private final RenderingConfig renderingConfig = new RenderingConfig();

	public IRichTextLabel()
	{
		super.setFont(FontTools.defaultFont);
		initGUI();
	}

	public IRichTextLabel(String text)
	{
		this.text = text;
		setFont(FontTools.defaultFont);
		initGUI();
	}

	public IRichTextLabel(Font font, Color c)
	{
		super.setFont(font);
		setForeground(c);
		initGUI();
	}

	public IRichTextLabel(String text, Font font, Color c)
	{
		setForeground(c);
		this.text = text;
		setFont(font);
		initGUI();
	}

	public IRichTextLabel(ICSetting iSetting)
	{
		super.setFont(FontTools.defaultFont);
		setText(iSetting);
		initGUI();
	}

	private void initGUI()
	{

	}

	@Override
	public void updateUI()
	{
		// setUI(new BasicLabelUI());
	}

	public void setLeftShadow(float x, float y, Color color)
	{
		leftX = x;
		leftY = y;
		leftColor = color;
	}

	public void setRightShadow(float x, float y, Color color)
	{
		rightX = x;
		rightY = y;
		rightColor = color;
		autoShadow = false;
	}

	public void setAutoShadow(boolean b)
	{
		autoShadow = b;
	}

	public void setParameter(String text, Font font, Color color)
	{
		super.setFont(font);
		setForeground(color);
		setText(text);
	}

	public void setParameter(ICSetting iSetting, Font font, Color color)
	{
		super.setFont(font);
		setForeground(color);
		setText(iSetting);
	}

	@Override
	public void setText(String text)
	{
		if (text == null || text.equals(this.text))
			return;
		this.text = text;
		preferredsize();
	}

	@Override
	public String getText()
	{
		return text;
	}

	@Override
	public void setFont(Font font)
	{
		if (font == null || font.equals(getFont()))
			return;
		if (autoShadow)
		{
			float fontSize = font.getSize();
			if (fontSize > 12)
				rightY = rightX = fontSize / 15;
			else
				rightY = rightX = 0.8f;
		}
		super.setFont(font);
		preferredsize();
	}

	private void preferredsize()
	{
		Font font = getFont();
		if (text == null || font == null)
			return;
		FontMetrics fm = this.getFontMetrics(font);
		hy = fm.getAscent();
		int w = fm.stringWidth(text);
		w += leftX + rightX;
		int h = fm.getHeight();
		h += leftY + rightY;
		preferredSize.width = w;
		preferredSize.height = h;
		setSize(w, h);
	}

	@Override
	public Dimension getPreferredSize()
	{
		if (text == null || getFont() == null)
			return super.getPreferredSize();
		return new Dimension(preferredSize);
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		if (text == null || text.isEmpty())
			return;
		Graphics2D g2d = (Graphics2D) g;
		Composite composite = ComponentTools.applyEnableAlpha(g2d, isEnabled(),
				1f, 0.3f);
		renderingConfig.setTextAADrawing(g2d);
		if (leftColor != null)
		{
			g.setColor(leftColor);
			g2d.translate(-leftX, -leftY);
			g.drawString(text, 0, hy);
			g2d.translate(leftX, leftY);
		}
		if (rightColor != null)
			g.setColor(rightColor);
		else
			g.setColor(RThemeColor.colorSet.shadowColor);
		g2d.translate(rightX, rightY);
		g.drawString(text, 0, hy);
		g2d.translate(-rightX, -rightY);
		g.setColor(getForeground());
		g.drawString(text, 0, hy);
		renderingConfig.resetTextAADrawing(g2d);
		if (composite != null)
			g2d.setComposite(composite);
	}
}
